package com.chencong.online.utils;

import com.chencong.online.common.ConfigCommon;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @program: bigdata
 * @ClassName RedisUtil
 * @description:Redis客户端线程池工具类
 * @author: chencong
 * @create: 2021-12-23 15:54
 **/
public class RedisUtil {
    public static JedisPool jedisPool = null;

    /**
     * @return
     * @Author chencong
     * @Description 获取jedis方法
     * @Date 16:37 2021/12/23
     * @Param
     **/
    public static Jedis getJedis() {
        if (jedisPool == null) {
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxTotal(100);//最大可用连接数
            jedisPoolConfig.setBlockWhenExhausted(true);//连接耗尽是否等待
            jedisPoolConfig.setMaxIdle(5);//最大闲置连接数
            jedisPoolConfig.setMinIdle(5);//最小闲置连接数
            jedisPoolConfig.setTestOnBorrow(true);//取连接的时候进行一下测试 ping pong

            jedisPool = new JedisPool(jedisPoolConfig, ConfigCommon.TEST135_REDIS_HOST, ConfigCommon.REDIS_PORT, 1000);

            System.out.println("开辟连接池成功！！");
            return jedisPool.getResource();
        } else {
            System.out.println("连接池活跃数量:" + jedisPool.getNumActive());
            return jedisPool.getResource();
        }
    }

}
